<?php
/*
+----------------------------------------------------------------------+
| (c) Copyright Silkwires. 					                           |
| 	All Rights Reserved.                                               |
+----------------------------------------------------------------------+
| > Module: .........                                                  |
| > Author: (SW Team)  ........  					                   |
| >	Class description	: Defined data that is retrieved from database,| 
						  prepare some methods from base object        |
+----------------------------------------------------------------------+
*/

class MotorSearch{
	
	private $db=null;
	private $config_site=null;
	
	function __construct(){
		$this->db=Zend_Registry::get('db');	
		$this->config_site=Zend_Registry::get('config_site');
	}
	
	function __destruct(){
		unset($this->db);
	}
    function getMotors($strWhere='', $strOrder='', $offset=0, $limit=0, $count=true){
        $query = 'SELECT ';
        if($count):
			$query .= 'SQL_CALC_FOUND_ROWS ';
		endif;
        $query.='m.id, m.user_id,
                m.alias,
                m.manu_id, m.model_id,
                m.city_id, m.type,
                m.`year`, m.kms,
                m.`desc`, m.accessories,
                m.image,m.image1,m.image2,m.price,
                m.price_from,m.price_to,
                m.cat_id,m.lang_id,
                m.admin_id,m.sort,
                m.premium,m.expired_date,
                m.created_by,m.created_time,
                m.updated_by,m.updated_time,
                m.active,m.style_id,
				m.manu_diffirent as manu_khac,
				m.model_diffirent as model_khac,
                c.`name` AS city_name,
                c.alias as city_alias,
                mf.`name` AS manu_name,
                mf.alias as manu_alias,
	            md.`name` AS model_name,
                md.alias as model_alias
                FROM city AS c
                INNER JOIN motor AS m ON c.id = m.city_id
                INNER JOIN manufacture AS mf ON m.manu_id = mf.id 
                INNER JOIN model AS md ON m.model_id=md.id
                WHERE 1 '.$strWhere.' '.$strOrder;
        if ($limit>0)
            $query  .= ' LIMIT '.$offset .','. $limit;
            #print($query);die;
        $rs = $this->db->fetchAll($query);
        if ($count){
            return array('total'=>$this->db->fetchOne('SELECT FOUND_ROWS()'), 'result'=>$rs);
        }else{
            return array('total'=>count($rs), 'result'=>$rs);
        }                               
    }
    function getDataByAlias ($table, $id){
        $query = 'SELECT * FROM '.$table.' WHERE alias=?';
        return $this->db->fetchRow($query, array ($id));
    }
}    